package com.yuedou.api.service.titlemenu.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuedou.api.dao.titlemenu.TitleMenuMapper;
import com.yuedou.api.entity.titlemenu.TitleMenu;
import com.yuedou.api.entity.wxuser.User;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.titlemenu.TitleMenuService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TitleMenuServiceImpl extends BaseServiceImpl<TitleMenuMapper, TitleMenu> implements TitleMenuService {

    @Resource
    private TitleMenuMapper titleMenuMapper;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Value("${web.imgPrefixOss}")
    private String imgPrefixOss;//图片前缀

    @Override
    public List<TitleMenu> selectByState(int state,int is_show_first,int menu_type,String platform) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",state);
            if(platform.equals("dymini")){
                qw.eq("is_show_dy",1);
            }
            qw.eq("is_show_first",is_show_first);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + menu_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return listMenu;
    }

    @Override
    public List<TitleMenu> selectByStateAndUser(User user, int state, int is_show_first, int menu_type,String platform) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",state);
            if(platform.equals("dymini")){
                qw.eq("is_show_dy",1);
            }
            qw.eq("is_show_first",is_show_first);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");
            String key = "title_menu_" + menu_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return listMenu;
    }

    @Override
    public List<TitleMenu> selectBySpecial(String category_type) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("is_show_first",0);
            qw.eq("menu_type",0);
            qw.eq("category_type",category_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + category_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return listMenu;
    }

    @Override
    public void resetMenuData(int menu_type) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("is_show_first",1);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + menu_type;

            listMenu = titleMenuMapper.selectList(qw);
            if(listMenu != null && listMenu.size() > 0){
                for(TitleMenu titleMenu : listMenu){
                    if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                        titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                    }
                }
            }
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void resetMenuDataSpecial(String service_type) {
        QueryWrapper<TitleMenu> qwSpecial = new QueryWrapper<>();
        qwSpecial.eq("state",0);
        qwSpecial.eq("menu_type",0);
        qwSpecial.eq("category_type",service_type);
        qwSpecial.orderByAsc("sort");

        String key = "title_menu_" + service_type;

        List<TitleMenu> listSpecialMenu = titleMenuMapper.selectList(qwSpecial);
        if(CollUtil.isNotEmpty(listSpecialMenu)){
            for(TitleMenu titleMenu : listSpecialMenu){
                if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                    titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                }
            }
        }
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listSpecialMenu));
    }
}
